package com.muqi.app.qlearn.student.ui;

import com.muqi.app.qlearn.modles.VedioInfo;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.id;
import com.muqi.app.qlearn.student.R.layout;
import com.muqi.app.qlearn.student.R.menu;
import com.universalvideoview.UniversalMediaController;
import com.universalvideoview.UniversalVideoView;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class VedioPlayActivity extends BaseFragmentActivity implements
		UniversalVideoView.VideoViewCallback {
	private UniversalVideoView mVideoView;
	private UniversalMediaController mMediaController;
	private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";
	private View mVideoLayout;
	private VedioInfo vedioInfo;
	private int mSeekPosition;
	private int cachedHeight;
	private boolean isFullscreen;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("===", mSeekPosition+"onCreate");
		setContentView(R.layout.activity_vedio_play);
		
	}

	@Override
	protected void onInit() {
		vedioInfo = (VedioInfo) getIntent().getSerializableExtra("vedioInfo");
		mVideoLayout = findViewById(R.id.video_layout);
		mVideoView = (UniversalVideoView) findViewById(R.id.videoView);
		mMediaController = (UniversalMediaController) findViewById(R.id.media_controller);
		mVideoView.setMediaController(mMediaController);

		setVideoAreaSize();
		mVideoView.setFullscreen(true);
		mVideoView.setVideoViewCallback(this);
		mVideoView
				.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					@Override
					public void onCompletion(MediaPlayer mp) {
						mVideoView.closePlayer();
						finish();
					}
				});
		mMediaController.setKeepScreenOn(true);
		mMediaController.mBackButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				mVideoView.closePlayer();
				finish();
				
			}
		});
	}

	@Override
	protected void onData() {
		if (mSeekPosition > 0) {
			mVideoView.seekTo(mSeekPosition);
		}
		Log.e("===", mSeekPosition+"原来播放的位置");
		mMediaController.setTitle(vedioInfo.danyuan_mingcheng);
		mVideoView.setVideoPath(vedioInfo.vedio_url);
		mVideoView.requestFocus();
		mVideoView.start();

	}

	/**
	 * 置视频区域大小
	 */
	private void setVideoAreaSize() {
		mVideoLayout.post(new Runnable() {
			@Override
			public void run() {
				int width = mVideoLayout.getWidth();
				cachedHeight = (int) (width * 405f / 720f);
				// cachedHeight = (int) (width * 3f / 4f);
				// cachedHeight = (int) (width * 9f / 16f);
				ViewGroup.LayoutParams videoLayoutParams = mVideoLayout
						.getLayoutParams();
				videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
				videoLayoutParams.height = cachedHeight;
				mVideoLayout.setLayoutParams(videoLayoutParams);

			}
		});
	}

	@Override
	public void onScaleChange(boolean isFullscreen) {
		this.isFullscreen = isFullscreen;
		if (isFullscreen) {
			ViewGroup.LayoutParams layoutParams = mVideoLayout
					.getLayoutParams();
			layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
			layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
			mVideoLayout.setLayoutParams(layoutParams);

		} else {
			ViewGroup.LayoutParams layoutParams = mVideoLayout
					.getLayoutParams();
			layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
			layoutParams.height = this.cachedHeight;
			mVideoLayout.setLayoutParams(layoutParams);

		}

	}

	@Override
	public void onPause(MediaPlayer mediaPlayer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStart(MediaPlayer mediaPlayer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBufferingStart(MediaPlayer mediaPlayer) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBufferingEnd(MediaPlayer mediaPlayer) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onPause() {
		super.onPause();
		
		if (mVideoView != null && mVideoView.isPlaying()) {
			mSeekPosition = mVideoView.getCurrentPosition();
			mVideoView.pause();
		}
		Log.e("===", mSeekPosition+"onPause");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.e("===", mSeekPosition+"onSaveInstanceState");
		outState.putInt(SEEK_POSITION_KEY, mSeekPosition);
	}

	@Override
	protected void onRestoreInstanceState(Bundle outState) {
		super.onRestoreInstanceState(outState);
		mSeekPosition = outState.getInt(SEEK_POSITION_KEY);
		Log.e("===", mSeekPosition+"onRestoreInstanceState");
	}

	@Override
	public void onBackPressed() {
		mVideoView.closePlayer();
		super.onBackPressed();
	}
       @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
    }
       @Override
    protected void onStart() {
    	// TODO Auto-generated method stub
    	super.onStart();
    	if (mSeekPosition > 0 && mVideoView!=null) {
			mVideoView.seekTo(mSeekPosition);
			mVideoView.requestFocus();
			mVideoView.start();
		}
    }
}
